package com.me.web.pubsub;

import com.alibaba.fastjson2.JSON;
import com.me.common.core.domain.AjaxResult;
import com.me.web.pubsub.queue.DeviceOtherQueue;
import com.me.web.pubsub.service.IMessagePublishService;
import com.me.web.pubsub.vo.CaffeineCacheMessage;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/pubsub")
public class PublishController {

    @Resource
    private IMessagePublishService messagePublishService;
    // http://localhost:8080/pubsub/publish?message=111&channel=prop_read
    // http://localhost:8080/pubsub/publish?message=111&channel=function_invoke
    // http://localhost:8080/pubsub/publish?message=111&channel=upgrade
    @GetMapping("/publish")
    public AjaxResult publish(@RequestParam String message, @RequestParam String channel) {
        CaffeineCacheMessage cacheMessage = new CaffeineCacheMessage();
        cacheMessage.setKey("AAA");
        cacheMessage.setValue("BBB");
        cacheMessage.setType(1);
        cacheMessage.setCacheName(message);
        messagePublishService.publish(JSON.toJSONString(cacheMessage), channel);
        return AjaxResult.success();
    }

    @GetMapping("/publish2")
    public AjaxResult publish2(@RequestParam String message) {
        CaffeineCacheMessage cacheMessage = new CaffeineCacheMessage();
        cacheMessage.setKey("AAA");
        cacheMessage.setValue("BBB");
        cacheMessage.setType(1);
        cacheMessage.setCacheName(message);
        DeviceOtherQueue.offer(cacheMessage);
        return AjaxResult.success();
    }
}